In [3]:
# librerias
import numpy as np

Crear Numpy Arrays

De una lista de python

Creamos el arreglo directamente de una lista o listas de python


In [4]:
my_list = [1,2,3]
my_list


Out[4]:
[1, 2, 3]

In [5]:
np.array(my_list)


Out[5]:
array([1, 2, 3])

In [6]:
my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
my_matrix


Out[6]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Métodos

arange


In [7]:
np.arange(0,10)


Out[7]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [8]:
np.arange(0,11,2)


Out[8]:
array([ 0,  2,  4,  6,  8, 10])

ceros y unos

Generar arreglos de ceros y unos


In [9]:
np.zeros(3)


Out[9]:
array([ 0.,  0.,  0.])

In [10]:
np.zeros((5,5))


Out[10]:
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

In [11]:
np.ones(3)


Out[11]:
array([ 1.,  1.,  1.])

In [12]:
np.ones((3,3))


Out[12]:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

linspace

Generar un arreglo especificando un intervalo


In [13]:
np.linspace(0,10,3)


Out[13]:
array([  0.,   5.,  10.])

In [14]:
np.linspace(0,10,50)


Out[14]:
array([  0.        ,   0.20408163,   0.40816327,   0.6122449 ,
         0.81632653,   1.02040816,   1.2244898 ,   1.42857143,
         1.63265306,   1.83673469,   2.04081633,   2.24489796,
         2.44897959,   2.65306122,   2.85714286,   3.06122449,
         3.26530612,   3.46938776,   3.67346939,   3.87755102,
         4.08163265,   4.28571429,   4.48979592,   4.69387755,
         4.89795918,   5.10204082,   5.30612245,   5.51020408,
         5.71428571,   5.91836735,   6.12244898,   6.32653061,
         6.53061224,   6.73469388,   6.93877551,   7.14285714,
         7.34693878,   7.55102041,   7.75510204,   7.95918367,
         8.16326531,   8.36734694,   8.57142857,   8.7755102 ,
         8.97959184,   9.18367347,   9.3877551 ,   9.59183673,
         9.79591837,  10.        ])

eye

Generar matrices de identidad


In [15]:
np.eye(4)


Out[15]:
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

Random

rand

Generar un arreglo con una forma determinada de numeros con una distribución uniforme [0,1]


In [16]:
np.random.rand(2)


Out[16]:
array([ 0.81970221,  0.0071643 ])

In [17]:
np.random.rand(5,5)


Out[17]:
array([[ 0.12406034,  0.84469205,  0.51966855,  0.01232607,  0.35625997],
       [ 0.43198096,  0.05682339,  0.78206935,  0.40022159,  0.6011775 ],
       [ 0.10350361,  0.18459984,  0.65828406,  0.43696651,  0.55443134],
       [ 0.10510654,  0.95735275,  0.87123229,  0.66183563,  0.25006296],
       [ 0.94456163,  0.32211027,  0.40780839,  0.44151584,  0.52810382]])

randn

Generar un arreglo con una distribucion estandar a diferencia de rand que es uniforme


In [18]:
np.random.randn(2)


Out[18]:
array([ 1.5502354 ,  0.95339967])

In [19]:
np.random.randn(5,5)


Out[19]:
array([[-1.287557  ,  1.17847417,  1.65041906, -1.1266163 ,  0.22100848],
       [-0.08979939, -0.87893085,  1.22119994, -1.25597172, -0.64581537],
       [ 0.55327412,  0.16685754, -0.46489372, -0.71493084, -0.32481316],
       [ 0.05398258,  1.34460242, -0.1856387 , -2.3508082 ,  0.06762043],
       [ 0.51167051, -0.8262621 , -1.0392656 , -0.21426112, -0.28889443]])

randint

Generar numeros aleatorios en un rango determinado


In [20]:
np.random.randint(1,100)


Out[20]:
81

In [21]:
np.random.randint(1,100,10)


Out[21]:
array([59, 14, 26, 56, 62, 52,  6, 70, 53, 21])

Metodos y atributos de arreglos


In [22]:
arr = np.arange(25)
ranarr = np.random.randint(0,50,10)

In [23]:
arr


Out[23]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24])

In [24]:
ranarr


Out[24]:
array([29,  7, 34, 48,  8, 13, 41,  0,  3, 14])

Reshape

Regresa el mismo arreglo pero en diferente forma


In [25]:
arr.reshape(5,5)


Out[25]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

max, min, argmax, argmin

Metodos para encontrar máximos y mínimos de los valores y sus indices


In [26]:
ranarr


Out[26]:
array([29,  7, 34, 48,  8, 13, 41,  0,  3, 14])

In [27]:
ranarr.max()


Out[27]:
48

In [28]:
ranarr.argmax()


Out[28]:
3

In [29]:
ranarr.min()


Out[29]:
0

In [30]:
ranarr.argmin()


Out[30]:
7

Shape

Atributo para desplegar la forma que tienen el arreglo


In [31]:
# Vector
arr.shape


Out[31]:
(25,)

In [32]:
# Tomar en cuenta que se implementan dos corchetes
arr.reshape(1,25)


Out[32]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24]])

In [33]:
arr.reshape(1,25).shape


Out[33]:
(1, 25)

In [34]:
arr.reshape(25,1)


Out[34]:
array([[ 0],
       [ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10],
       [11],
       [12],
       [13],
       [14],
       [15],
       [16],
       [17],
       [18],
       [19],
       [20],
       [21],
       [22],
       [23],
       [24]])

In [35]:
arr.reshape(25,1).shape


Out[35]:
(25, 1)

dtype

Despliega el tipo de dato de los objetos del arreglo


In [38]:
arr.dtype


Out[38]:
dtype('int64')

In [ ]: